package com.ama.controller;

import com.ama.dao.EstateDao;
import com.ama.dao.RepairDao;
import com.ama.pojo.Estate;
import com.ama.pojo.Property;
import com.ama.pojo.Repair;
import com.ama.service.PropertyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class RepairController {
    @Autowired
    EstateDao estateDao;
    @Autowired
    RepairDao repairDao;
    @Autowired
    PropertyService propertyService;

    @RequestMapping("/repairSubmit")
    public String repairSubmit(HttpServletRequest request){
        String address = request.getParameter("address");
        String type = request.getParameter("type");
        String manager = request.getParameter("manager");
        String phone =request.getParameter("phone");
        String tips = request.getParameter("tips");
        String[] split = address.split("-");
        String estateName = split[0] ;

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("estate_name",estateName);
        Estate estate = estateDao.selectOne(wrapper);
        Long EstateId = estate.getEstateId();
        Property property = propertyService.selectById(estate.getPropertyId());

        Repair repair = new Repair();
        repair.setEstateId(estate.getEstateId());
        repair.setPropertyId(property.getPropertyId());
        repair.setRepairType(type);
        repair.setOwnerName(manager);
        repair.setRepairAddress(address);
        repair.setPhoneNumber(phone);
        repair.setRepairInfo(tips);
        repair.setRepairState(0);
        Date date = new Date();
        repair.setRepairDate(date);
        repairDao.insertRepair(repair );
        return "redirect:/mainpage.jsp";
    }

    @RequestMapping("/repairSuccess")
    public String repairSuccess(HttpServletRequest request){
        Long repairId = Long.valueOf(request.getParameter("repairId"));
        Repair repair = repairDao.selectById(repairId);
        repair.setRepairState(1);
        repairDao.update(repair,null);
        return "redirect:/success/repair_suc.jsp";
    }

}
